
/**
 * Copyright 2009 cestum.org
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
package org.waveprotocol.wave.model.wave.data.impl {
import org.goverla.collections.HashMap;
import org.waveprotocol.wave.model.id.WaveId;
import org.waveprotocol.wave.model.id.WaveletId;

import org.waveprotocol.wave.model.wave.data.WaveViewData;
import org.waveprotocol.wave.model.wave.data.WaveletData;

public class WaveViewDataImpl implements WaveViewData {

  /** The wave id */
  private var id:WaveId;

  /** Wavelets in this wave. */
  private var wavelets:HashMap;

  /**
   * Creates a new wave.
   */
  public function WaveViewDataImpl(id:WaveId) {
    this.id = id;
    this.wavelets = new HashMap();
  }

  
  public function getWaveId():WaveId {
    return id;
  }

  
  public function getWavelets():Array {
    return wavelets.values;
  }

  
  public function getWavelet(waveletId:WaveletId):WaveletData {
    return WaveletDataImpl(wavelets.getItem(waveletId.getId()));
  }

  
  public function createWavelet(waveletId:WaveletId):WaveletData {
    if (wavelets.containsKey(waveletId.getId())) {
      throw new Error("Duplicate wavelet id: " + waveletId);
    }
    var newWavelet:WaveletDataImpl= new WaveletDataImpl(id, waveletId);
    wavelets.addItem(waveletId.getId(), newWavelet);
    return newWavelet;
  }

  
  public function removeWavelet(waveletId:WaveletId):void {
    if (wavelets.removeItemAt(waveletId.getId()) == null) {
      throw new Error(waveletId + " is not present");
    }
  }
}
}